var PublicPostHandler = function (){
     var csrf_token = $("meta[name='csrf-token']").attr("content");
     var editor = new window.wangEditor('#editor');
     editor.config.uploadImgServer = "/post/image/upload";
     editor.config.uploadFileName = "image";
     //上传csrf的位置
     //1.请求体
    //2.请求头 X-CSRFToken
    //和cookie中的csrf_token 进行对比
     editor.config.uploadImgHeaders = {
         "X-CSRFToken": csrf_token
     }
     editor.config.uploadImgMaxSize = 1024*1024*5
     this.editor = editor;
     this.editor.create();
}

PublicPostHandler.prototype.listenSubmitEvent = function (){
    var that = this;
    $("#submit-btn").on('click',function (event){
        event.preventDefault();
        var title = $("input[name='title']").val();
        var board_id =$("select[name='board_id']").val();
        var content = that.editor.txt.html();
        forum.post({
            url:"/post/public",
            data:{
                title,board_id,content
            },
            success:function (result){
                if(result['code']===200){
                    let post_id = result['data']['id'];
                    window.location = '/post/detail/'+post_id;
                } else{
                    alert(result['message']);
                }
            }
        })
    })
}

PublicPostHandler.prototype.run = function (){
    this.listenSubmitEvent();
}

$(function (){
    var handler = new PublicPostHandler();
    handler.run();
})